package com.wedo.mall.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.e.library.entity.User;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.BuildConfig;
import com.wedo.mall.R;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.util.WUtils;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lxy on 2017/6/25.
 */

public class PaymentView extends LinearLayout {

    public static final int PAYMENT_PAYPAL = 0;
    public static final int PAYMENT_WECHAT = 1;
    public static final int PAYMENT_ALIPAY = 2;
    public static final int PAYMENT_CREDIT = 3;
    public static final int PAYMENT_BALANCE = 4;
    public static final int PAYMENT_IPAY88 = 5;

    private int payment = PAYMENT_IPAY88;

    @BindView(R.id.tv_balance) TextView tv_balance;
    @BindView(R.id.payment_balance) View payment_balance;
    @BindViews({R.id.iv_paypal,R.id.iv_wechat,R.id.iv_alipay,R.id.iv_credit,R.id.iv_balance,R.id.iv_ipay88})
    List<View> ivs;

    @BindString(R.string.payment_balance) String balance_format;

    private double mBalance = 0.00;
    private String pay_password;

    public PaymentView(Context context) {
        super(context);
        init(context);
    }

    public PaymentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PaymentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_payment,null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        this.addView(view,params);
        ButterKnife.bind(this);
        onPaymentChanged();
        if(BuildConfig.DEBUG) {
            payment_balance.setVisibility(VISIBLE);
        }

    }

    @OnClick({R.id.payment_ipay88,R.id.payment_paypal,R.id.payment_wechat,R.id.payment_alipay,R.id.payment_credit,R.id.payment_balance})
    protected void onPaymentClick(View view){
        switch (view.getId()){
            case R.id.payment_paypal:
                payment = PAYMENT_PAYPAL;
                break;
            case R.id.payment_wechat:
                payment = PAYMENT_WECHAT;
                break;
            case R.id.payment_alipay:
                payment = PAYMENT_ALIPAY;
                break;
            case R.id.payment_credit:
                payment = PAYMENT_CREDIT;
                break;
            case R.id.payment_balance:
                payment = PAYMENT_BALANCE;
                break;
            case R.id.payment_ipay88:
                payment = PAYMENT_IPAY88;
                break;
        }

        onPaymentChanged();
    }

    private void onPaymentChanged(){
        for (View v:ivs){
            v.setVisibility(GONE);
        }
        ivs.get(payment).setVisibility(VISIBLE);
    }

    public void balance(double balance){
        balance(String.valueOf(balance));
    }

    public void balance(String balance){
        if(TextUtils.isEmpty(balance)) {
            mBalance = 0.00;
        }
        mBalance = WUtils.toDouble(balance);
        tv_balance.setText(String.format(balance_format,balance));
    }

    public boolean balanceEnough(String amount){
        double a = WUtils.toDouble(amount);
        return balanceEnough(a);
    }

    /**余额是否充足*/
    public boolean balanceEnough(double amount){
        if (mBalance < amount) {
            return false;
        }
        return true;
    }

    public int payment(){
        return payment;
    }

    public boolean paypal(){return payment == PAYMENT_PAYPAL;}
    public boolean wechat(){return payment == PAYMENT_WECHAT;}
    public boolean alipay(){return payment == PAYMENT_ALIPAY;}
    public boolean credit(){return payment == PAYMENT_CREDIT;}
    public boolean balance(){return payment == PAYMENT_BALANCE;}
    public boolean iPay88() {
        return payment == PAYMENT_IPAY88;
    }

    /**获取个人账户金额*/
    private void account(){
        if (!WLogin.logged(getContext())){
            balance(0.00);
            return;
        }
        EClient.with().post(EApi.USER_INFO, new EObserver() {
            @Override
            public void successful(EResponse response) {
                User user = response.object("list",User.class);
                pay_password = user.pay_password;
                PaymentView.this.balance(user.money);
            }

            @Override
            public void failure(String msg) {
                PaymentView.this.balance(0.00);
            }

        });
    }

    public void onResume(){
        account();
    }

    public void recharge(){
        payment_balance.setVisibility(GONE);
    }

    /**不等于空表示已设置了支付密码*/
    public boolean hasPassword(){
        return !TextUtils.isEmpty(pay_password);
    }

    /**解决bug*/
    public void onSetPassword(String password){
        pay_password = password;
    }

}
